package com.example.limit.mapper;

import com.example.limit.entity.PurchaseRecord;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface PurchaseRecordMapper {
    
    @Insert("INSERT INTO purchase_record(user_id, product_id, category_id, quantity, order_id, purchase_time, create_time) " +
            "VALUES(#{userId}, #{productId}, #{categoryId}, #{quantity}, #{orderId}, #{purchaseTime}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(PurchaseRecord record);
    
    @Select("SELECT SUM(quantity) FROM purchase_record WHERE user_id=#{userId} AND product_id=#{productId} " +
            "AND purchase_time >= #{startTime} AND purchase_time <= #{endTime}")
    Integer sumQuantityByUserAndProduct(Long userId, String productId, LocalDateTime startTime, LocalDateTime endTime);
    
    @Select("SELECT SUM(quantity) FROM purchase_record WHERE user_id=#{userId} AND category_id=#{categoryId} " +
            "AND purchase_time >= #{startTime} AND purchase_time <= #{endTime}")
    Integer sumQuantityByUserAndCategory(Long userId, String categoryId, LocalDateTime startTime, LocalDateTime endTime);
    
    @Select("SELECT SUM(quantity) FROM purchase_record WHERE user_id=#{userId} " +
            "AND purchase_time >= #{startTime} AND purchase_time <= #{endTime}")
    Integer sumQuantityByUser(Long userId, LocalDateTime startTime, LocalDateTime endTime);
    
    @Select("SELECT * FROM purchase_record WHERE order_id=#{orderId}")
    List<PurchaseRecord> selectByOrderId(Long orderId);
}